/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.utils;

import com.toedter.calendar.JDateChooser;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import researchgrants.parts.LoggedData.panels.events.ChangeEventHandler;
import javax.swing.event.ChangeListener;

/**
 *
 * @author DOStudent1
 */
public class DateRangeSelector extends JPanel implements ChangeEventHandler {

    public DateRangeSelector() {
        addContent();
    }
    
    JDateChooser fromDateChooser, toDateChooser;

    private void addContent() {
        setLayout(new GridLayout(2, 2));

        add(new JLabel("From:"));
        fromDateChooser=new JDateChooser();
        DateChooserListener fromListener=new DateChooserListener();
        fromDateChooser.addPropertyChangeListener("date", fromListener);
        add(fromDateChooser);

        add(new JLabel("To:"));
        toDateChooser=new JDateChooser();
        DateChooserListener toListener=new DateChooserListener();
        toDateChooser.addPropertyChangeListener("date", toListener);
        add(toDateChooser);

    }
    
    Collection<ChangeListener> eventListeners=new LinkedList<ChangeListener>();

    public void addChangeListener(ChangeListener changeEventListener) {
        eventListeners.add(changeEventListener);
    }

    private void notifyEventListeners() {
        for (Iterator<ChangeListener> it = eventListeners.iterator(); it.hasNext();) {
            ChangeListener changeEventListener = it.next();
            changeEventListener.stateChanged(new ChangeEvent(this));
        }
    }
    
    public Date getFromDate() {
        return(fromDateChooser.getDate());
    }

    public Date getToDate() {
        return(toDateChooser.getDate());
    }

    private class DateChooserListener implements PropertyChangeListener {

        public DateChooserListener() {
        }

        public void propertyChange(PropertyChangeEvent evt) {
            notifyEventListeners();
        }

    }


}
